package cn.lili.controller.user.union;

import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.distribution.entity.vos.HomeResult;
import cn.lili.modules.member.entity.dos.Union;
import cn.lili.modules.member.entity.dos.UnionMember;
import cn.lili.modules.member.entity.dos.UnionMemberApply;
import cn.lili.modules.member.entity.dto.UnionAddDTO;
import cn.lili.modules.member.entity.dto.UnionMemberCountDTO;
import cn.lili.modules.member.entity.dto.UnionMemberDetailDTO;
import cn.lili.modules.order.order.entity.dos.OrderFlow;
import cn.lili.modules.order.order.entity.dto.OrderFlowDTO;
import cn.lili.modules.order.order.entity.vo.PerformanceDetailVO;
import cn.lili.modules.order.order.entity.vo.UnionOrderParams;
import cn.lili.modules.union.service.UnionMemberService;
import cn.lili.modules.union.service.UnionService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "工会成员管理接口")
@RestController
@RequestMapping("/user/union/member")
@RequiredArgsConstructor
@Validated
public class UnionMemberController {

    private final UnionMemberService unionMemberService;


    @ApiOperation(value = "获取工会会长信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "unionId", value = "工会ID", required = true,paramType = "query"),
    })
    @GetMapping(value = "/chairman")
    public ResultMessage<UnionMember> getUnionChairman(String unionId) {
        return ResultUtil.data(unionMemberService.getUnionChairman(unionId));
    }


    @ApiOperation(value = "分页获取工会成员列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页对象,含pageNumber(页号),pageSize(每页大小)", required = true, paramType = "query"),
            @ApiImplicitParam(name = "unionId", value = "工会ID", required = true,paramType = "query"),
    })
    @GetMapping(value = "/list")
    public ResultMessage<Page<UnionMember>> getMembersByPage(String unionId, PageVO page) {
        return ResultUtil.data(unionMemberService.getMembersByPage(unionId, page));
    }

    @ApiOperation(value = "分页获取工会成员列表")
    @GetMapping()
    public ResultMessage<Page<UnionMember>> getByPage(UnionMember unionMemberParams, PageVO pageVO) {
        return ResultUtil.data(unionMemberService.getByPage(unionMemberParams, pageVO));
    }

    @ApiOperation(value = "根据工会ID获取工会成员统计数据，如成员数量，本月新增，上月新增")
    @ApiImplicitParam(name = "unionId", value = "工会ID", required = true, paramType = "path", dataType = "String")
    @GetMapping(value = "/countBy/{unionId}")
    public ResultMessage<UnionMemberCountDTO> getUnionById(@Valid @PathVariable String unionId) {
        return ResultUtil.data(unionMemberService.getCountData(unionId));
    }

    @ApiOperation(value = "踢出成员")
    @PostMapping("/deleteUnionMember")
    public ResultMessage<Page<UnionMember>> deleteUnionMember(@RequestBody UnionAddDTO unionAddDTO) {
        unionMemberService.deleteUnionMember(unionAddDTO);
        return ResultUtil.success();
    }

    @ApiOperation(value = "审核成员")
    @GetMapping("/apply")
    public ResultMessage<Page<UnionMember>> apply(String unionId, String memberId, String status) {
        unionMemberService.apply(unionId, memberId, status);
        return ResultUtil.success();
    }

    @ApiOperation(value = "分页查询工会成员本月业绩")
    @GetMapping("/getCurrentMonthByUnionMember")
    public ResultMessage<Page<OrderFlowDTO>> getCurrentMonthByUnionMember(UnionOrderParams params) {
        return ResultUtil.data(unionMemberService.getCurrentMonthByUnionMember(params));
    }

    @ApiOperation(value = "分页查询工会成员本月业绩统计")
    @GetMapping("/getCurrentMonthStatisticsByMember")
    public ResultMessage<PerformanceDetailVO> getCurrentMonthStatisticsByMember(UnionOrderParams params) {
        return ResultUtil.data(unionMemberService.getCurrentMonthStatisticsByMember(params));
    }

    @ApiOperation(value = "获取成员详情,包括成员基本信息，上月业绩、订单数、成交人数，本月业绩、订单数、成交人数")
    @ApiImplicitParam(name = "memberId", value = "工会成员ID", required = true, paramType = "path", dataType = "String")
    @GetMapping(value = "/detail/{memberId}")
    public ResultMessage<UnionMemberDetailDTO> getMemberInfoById(@Valid @PathVariable String memberId) {
        UnionMemberDetailDTO result = unionMemberService.getDetailInfo(memberId);
        if(null==result){
            return ResultUtil.error(1,"未加入工会");
        }else{
            return ResultUtil.data(result);
        }

    }

    @ApiOperation(value = "分页查询工会成员上月业绩")
    @GetMapping("/getLastMonthByUnionMember")
    public ResultMessage<Page<OrderFlowDTO>> getLastMonthByUnionMember(UnionOrderParams params) {
        return ResultUtil.data(unionMemberService.getLastMonthByUnionMember(params));
    }

    @ApiOperation(value = "分页查询工会成员上月业绩统计")
    @GetMapping("/getLastMonthStatisticsByMember")
    public ResultMessage<PerformanceDetailVO> getLastMonthStatisticsByMember(UnionOrderParams params) {
        return ResultUtil.data(unionMemberService.getLastMonthStatisticsByMember(params));
    }

    @ApiOperation(value = "添加工会成员")
    @PostMapping("/addMember")
    public ResultMessage<Object> addMember (@RequestBody UnionAddDTO unionAddDTO) {
        unionMemberService.addMember(unionAddDTO);
        return ResultUtil.success();
    }

    /**
     * 店主数量统计
     * @return 店主数量统计
     */
    @GetMapping("/shopkeeper/statistics")
    public ResultMessage<HomeResult> shopkeeperStatistics() {
        return unionMemberService.shopkeeperStatistics();
    }

}
